﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinesys.Domain.InputValidations.ComponentModel;
using System.Text.RegularExpressions;

namespace Chinesys.Domain.InputValidations.Common
{
    public class EmailAddress : ValidationBase
    {
        private const string _emailRegex = @"^(?("""")("""".+?""""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$";
        private Func<string> _emailGetter;

        public EmailAddress(Func<string> emailGetter)
        {
            _emailGetter = emailGetter;
        }

        public override string Message
        {
            get
            {
                return "El formato de la direccion de email es incorrecto.";
            }
        }

        public override bool IsNotValid()
        {
            string email = _emailGetter();

            if (email == null)
                return false;
            else
                return !Regex.IsMatch(email, _emailRegex);
        }
    }
}
